package cn.mw.cmdb.util;

import cn.mw.cmdb.param.ItemNameParam;
import com.google.common.base.Strings;

public class ItemNameFilterUtil {

    /**
     * 截取item监控项
     * devName 监控项设备（包含括号）
     * itemName 监控项名称
     * @param name
     * @return
     */
    public static ItemNameParam itemNameFilterBracket(String name) {
        ItemNameParam itemNameParam = new ItemNameParam();
        if (Strings.isNullOrEmpty(name)) {
            return itemNameParam;
        }
        String devName = "";
        String itemName = "";
        if (name.indexOf("<") != -1 && name.indexOf("[") != -1) {
            devName = name.substring(0, name.indexOf(">") + 1);
            itemName = name.substring(name.indexOf(">") + 1);
        } else if (name.indexOf("<") == -1 && name.indexOf("[") != -1) {
            devName = name.substring(0, name.lastIndexOf("]") + 1);
            itemName = name.substring(name.lastIndexOf("]") + 1);
        }else {
            itemName = name;
            devName = "";
        }
        itemNameParam.setDevName(devName);
        itemNameParam.setItemName(itemName);
        return itemNameParam;
    }

    /**
     * 截取item监控项
     * devName 监控项设备（不包含括号）
     * itemName 监控项名称
     * @param name
     * @return
     */
    public static ItemNameParam itemNameFilter(String name) {
        ItemNameParam itemNameParam = itemNameFilterBracket(name);
        itemNameParam.setDevName(strSubSplit(itemNameParam.getDevName()));
        return itemNameParam;
    }

    /**
     * 字符串截取
     */
    private static String strSubSplit(String str){
        String subStr = "";
        if(!Strings.isNullOrEmpty(str) && str.length() > 1 ){
            subStr = str.substring(1,str.length() - 1);
        }
        return subStr;
    }

}
